/**
 * created by 小卷毛, 2020-05-06 006
 * Copyright (c) 2020, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
@file:Suppress("NOTHING_TO_INLINE")

package com.zhiyou.aifeng.common.helper

import android.content.Context
import androidx.fragment.app.Fragment

/**
 * SharedPreferences扩展工具类
 */

inline fun Context.getString(key: String, default: String = ""): String =
    PreferencesHelper.get(context = this, key = key, defalutValue = default)

inline fun Context.getBoolean(key: String): Boolean =
    PreferencesHelper.get(context = this, key = key, defalutValue = false)

inline fun Context.getInt(key: String, default: Int = -1): Int =
    PreferencesHelper.get(context = this, key = key, defalutValue = default)

inline fun <T : Any> Context.putShared(key: String, value: T) =
    PreferencesHelper.put(
        context = this,
        key = key,
        value = when (value) {
            is String -> if (value == "null") "" else value
            else -> value
        }
    )

inline fun Context.clearShared(vararg keys: String, value: String = "") =
    keys.forEach { putShared(it, value) }

inline fun Fragment.getString(key: String, default: String = ""): String =
    PreferencesHelper.get(this.activity!!, key, default)

inline fun Fragment.getBoolean(key: String, default: Boolean = false): Boolean =
    PreferencesHelper.get(this.activity!!, key, default)

inline fun Fragment.getInt(key: String, default: Int = -1): Int =
    PreferencesHelper.get(this.activity!!, key, default)

inline fun <T : Any> Fragment.putShared(key: String, value: T) =
    PreferencesHelper.put(
        context = this.activity!!,
        key = key,
        value = when (value) {
            is String -> if (value == "null") "" else value
            else -> value
        }
    )

/**
 * kotlin 扩展属性 判断是否登录
 */
var Context.isLogin: Boolean
    get() {
        val isLogin by PreferenceDelegate<Boolean>(
            context = this,
            name = "isLogin",
            default = false
        )
        return isLogin
    }
    set(value) {
        var isLogin by PreferenceDelegate<Boolean>(
            context = this,
            name = "isLogin",
            default = false
        )
        isLogin = value
    }